from Quoridor_game import QuoridorAction, DIRECTIONS
from game_agent import GameAgent
from quoridor_agents import AGENTS
import random
import compare_agents

PAUSES = False

class DummyAgent(GameAgent):
    def move(self, game_state):
        actions = game_state.getSuccessors().keys()        
        actionNdx = random.randrange(len(actions))
        action = actions[actionNdx]
        
        if (PAUSES):
            raw_input('enter to continue...')
        
        return action
    
    def setup(self, player, game_state, turn_time_limit, setup_time_limit):
        random.seed()
        
        print 'Setup for Dummy player: ' + player

class InteractiveAgent(GameAgent):
    def move(self, game_state):
        ok = False        
        while not ok:
            print self.player + ' player\'s turn'
            print 'to move a piece: "M pieceNdx N/S/W/E" \n'
            print 'to place a vertical/horizontal wall: "WV/WH majorNdx minorNdx" \n'
            inp = raw_input ('your selection: ')
            actionParams = inp.split()
     
            try:
                if len(actionParams) != 3 or actionParams[0] not in QuoridorAction.LegalTypes:
                    raise Exception()
                type = actionParams[0]            
                if (type == 'M'):
                    dir = actionParams[2]                    
                    pieceNdx = int(actionParams[1])
                    if ((dir not in DIRECTIONS) or (pieceNdx not in xrange(game_state.numPieces))):                        
                        raise Exception()                    
                    info = [pieceNdx, dir]
                else:
                    info = [int(actionParams[1]),int(actionParams[2])]
                action = QuoridorAction(type, info)
            except:
                print 'Bad format\n\n'
                continue            
            ok = True

        return action           

    def setup(self, player, game_state, turn_time_limit, setup_time_limit):        
        self.player = player
        print 'Setup for Interactive player: ' + player


agents = {}


results = compare_agents.playTournament(AGENTS, [7], [15])

compare_agents.printSummary(results)
compare_agents.exportResultsToCSV("critical_move_updating.csv",results)
